package com.h5lib.nchuoffice.app.action;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.h5lib.nchuoffice.CacheManager;
import com.h5lib.nchuoffice.NchuOfficeUtils;
import com.h5lib.nchuoffice.app.cfg.Config;
import com.h5lib.nchuoffice.model.Class_;
import com.h5lib.nchuoffice.model.Course;
import com.h5lib.server.ModelMap;
import com.h5lib.server.annotation.Controller;
import com.h5lib.server.annotation.RequestMapping;

import fi.iki.elonen.NanoHTTPD.IHTTPSession;
import fi.iki.elonen.NanoHTTPD.Response;
import fi.iki.elonen.NanoHTTPD.Response.Status;

@Controller
public class CourseAction {

	
	@RequestMapping(value="/course.html")
	public String course(IHTTPSession session, Response response ,ModelMap modelMap){
		Object userInfo = CacheManager.getCache(Config._CACHE_USERINFO);
		modelMap.put("userInfo", userInfo);
		try {

			String cacheKey = Config._CACHE_BASEDATA;
			if(CacheManager.getCache(cacheKey) == null){
				Map<String, Object> baseData = NchuOfficeUtils.getBaseData();
				CacheManager.addCache(cacheKey, baseData);
			}
			modelMap.put("baseData", CacheManager.getCache(cacheKey));
		} catch (Exception e) {
			e.printStackTrace();
			modelMap.put("err", "基础信息获取异常！");
			return "/ui/error.html";
		}
		return "/ui/course.html";
	}
	
	/**
	 * 查询课程
	 * @param session
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value="/doQueryCourse")
	public Response doQueryCourse(IHTTPSession session, Response response ,ModelMap modelMap){
		try {
			String schoolTerm = session.getParms().get("schoolTerm");
			String institute = session.getParms().get("institute");
			String _class = session.getParms().get("_class");
			String year = session.getParms().get("year");
			if(_class == null 
					|| schoolTerm == null 
					|| institute == null 
					|| "".equals(_class) 
					|| "".equals(schoolTerm)
					|| "".equals(institute)
					){
				throw new Exception("查询参数不能为空");
			}
			else
			{
				List<Course> courses = null;
				// 缓存Key
				String cacheKey = Config._CACHE_COURSE+"_"+session.getQueryParameterString();
				if(CacheManager.getCache(cacheKey) == null){
					courses = NchuOfficeUtils.getCoursesByClass(year,institute,_class,schoolTerm);
					CacheManager.addCache(cacheKey, courses);
				}
				modelMap.put("err", "0");
				modelMap.put("courses", CacheManager.getCache(cacheKey));
			}

		} catch (Exception e) {
			e.printStackTrace();
			modelMap.put("err", "-1");
		}
		
		return new Response(Status.OK, "text/plain", JSON.toJSONString(modelMap.getMap()));
	}
	
	@RequestMapping(value="/doQueryClass")
	public Response doQueryClass(IHTTPSession session, Response response ,ModelMap modelMap){
		try {
			String schoolTerm = session.getParms().get("schoolTerm");
			String institute = session.getParms().get("institute");
			String year = session.getParms().get("year");
			if(
					   schoolTerm == null 
					|| institute == null 
					|| "".equals(schoolTerm)
					|| "".equals(institute)
					){
				throw new Exception("查询参数不能为空");
			}
			else
			{
				List<Class_> classes = null;
				// 缓存Key
				String cacheKey = Config._CACHE_CLASSES+"_"+session.getQueryParameterString()+"-";
				if(CacheManager.getCache(cacheKey) == null){
					classes = NchuOfficeUtils.getClasses(year,institute,schoolTerm);
					CacheManager.addCache(cacheKey, classes);
				}
				modelMap.put("err", "0");
				modelMap.put("classes", CacheManager.getCache(cacheKey));
			}

		} catch (Exception e) {
			e.printStackTrace();
			modelMap.put("err", "-1");
		}
		
		return new Response(Status.OK, "text/plain", JSON.toJSONString(modelMap.getMap()));
	}
	
}
